﻿/*
* Add all Article into basket JavaAScript.
*/

var AddAllIntoBasket = {

    AddAllUrl: JsConst.AddAllIntoBasketUrl,

    List: [],

    SelectArticlesMsg : JsConst.ErrorMsg_AddAllIntoBasket_SelectArticles,

    Do: function (btn) {
        if (this.List != null && this.List.length > 0) {
            var self = this;
            var json = "";
            $.each(self.List, function (idx, priceObj) {
                if (priceObj != null) {
                    if (priceObj.IsQuantitySet()) {
                        var quantity = priceObj.GetQuantityFloat();
                        if (quantity > 0) {
                            var articlePrice = priceObj.GetArticlePriceBySelOpt();
                            if (articlePrice != null) {
                                var article = priceObj.GetArticleInputModel(articlePrice, quantity);
                                if (article != null) {
                                    json = $.toJSON(article) + "," + json;
                                }
                            }
                        }
                    }
                }
            });
            if (json != "") {
                this.BlockBtn($(btn), true);
                json = "[" + json.substr(0, json.length - 1) + "]";
                //alert(json);
                $.ajax({
                    url: self.AddAllUrl,
                    cache: false,
                    type: 'POST',
                    dataType: 'json',
                    data: json,
                    contentType: 'application/json; charset=utf-8',
                    success: function (data) {
                        //For Properties Info see Biosaleavis.Models.JsonOutputModel
                        self.AddAll_callback(data, btn);
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        self.AddAll_callback(xhr.status + ":" + thrownError, btn);
                    }
                });
            }
            else {
                alert(this.SelectArticlesMsg);
            }
        }
    },

    AddAll_callback: function (callBackData, btn) {
        this.BlockBtn($(btn), false);
        if (typeof (callBackData) == "object") {
            Dialog.ShowAddToBasket();
            if (typeof (Basket) == "object") Basket.Refresh();
        }
        else {
            alert(callBackData);
        }
    },

    BlockBtn: function (btn, val) {
        if (btn != null) {
            if (val == true) {
                btn.attr('disabled', true);
            }
            else {
                btn.attr('disabled', false);
            }
        }
    }
}